#include "LogicData.h"
#include "LogicConfig.h"
#include "LogicRecvThread.h"

#include "logicbase/GLLogicManager.h"
#include "common/PThreadManager.h"

#include "LLogicLogin.h"
#include "LLogicContainer.h"
#include "LLogicEntity.h"
#include "LLogicScene.h"
#include "LLogicTask.h"
#include "LLogicProperty.h"
#include "LLogicReward.h"
#include "LLogicGroup.h"
#include "LLogicMap.h"
#include "LLogicSkill.h"

#include "logicbase/GLGoodsBase.h"
#include "entity/GLGoodsCommodity.h"
#include "entity/GLGoodsProperty.h"
#include "entity/GLGoodsTrade.h"
#include "entity/GLGoodsWeapon.h"

#include <assert.h>

#define LOGIC_INIT(Class) \
	Class* t##Class = new Class; \
	t##Class->LogicInit(); \
	CGLLogicManager::GetInstance()->RegisterLogic(t##Class);

QUEUE_OBJ_PTR_FUNC_INIT_2(LogicData, CGLGoodsBase, Goods)
QUEUE_OBJ_PTR_FUNC_INIT_2(LogicData, CGLGoodsWeapon, Weapon)
QUEUE_OBJ_PTR_FUNC_INIT_2(LogicData, CGLGoodsCommodity, Commodity)
QUEUE_OBJ_PTR_FUNC_INIT_2(LogicData, CGLGoodsProperty, GoodsProp)
QUEUE_OBJ_PTR_FUNC_INIT_2(LogicData, CGLGoodsTrade, GoodsTrade)

CLogicData::CLogicData(void) : m_Oneself(nullptr), m_Group(nullptr)
{
	m_ChangeStatus.resize(CSTotalCount, 0);
}


CLogicData::~CLogicData(void)
{
	if (nullptr != m_Oneself) delete m_Oneself;
}

bool CLogicData::InitBase() {
	return true;
}

bool CLogicData::InitInfo() {
	return true;
}

bool CLogicData::Init() {
	if (!CLogicConfig::GetInstance()->Init()) {
		assert(false);
		return false;
	}
	if (!LSocket::InitSock()) {
		assert(false);
		return false;
	}
	if (!m_LogicConnect.Connect(CLogicConfig::GetInstance()->m_LogicIp, CLogicConfig::GetInstance()->m_LogicPort)) {
		assert(false);
		return false;
	}

	CPThreadManager::Getinstance()->AddThread<CLogicRecvThread>(true);

	m_Oneself = new CILogicPerson;
	m_Oneself->Init();

	CGLLogicManager* tLogicManager = CGLLogicManager::GetInstance();
	LOGIC_INIT(CTLLogicLogin);
	LOGIC_INIT(CTLLogicTask);
	LOGIC_INIT(CTLLogicContainer);
	LOGIC_INIT(CTLLogicScene);
	LOGIC_INIT(CLLogicProperty);
	LOGIC_INIT(CLLogicReward);
	LOGIC_INIT(CLLogicGroup);
	LOGIC_INIT(CLLogicMap);
	LOGIC_INIT(CLLogicSkill);
	return true;
}

void CLogicData::AddMyBody(uint64_t bid, CGLBody* body) {
	size_t tSize = m_MyBodys.size();
	m_MyBodys[bid] = body;
	if (m_MyBodys.size() == tSize) {
		CCLOG("error|add|my|body");
	}
}

void CLogicData::DelMyBody(uint64_t bid) {
	m_MyBodys.erase(bid);
}

CGLBody* CLogicData::GetMyBody(uint64_t bid) const {
	MapBody_t::const_iterator tIter = m_MyBodys.find(bid);
	if (tIter != m_MyBodys.end())
		return tIter->second;
	return nullptr;
}
